package src.view;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import javax.swing.*;

import com.jgoodies.forms.layout.*;
import org.jdesktop.swingx.*;
/*
 * Created by JFormDesigner on Sat May 29 18:27:05 ART 2010
 */

import src.controller.AddMailBoxController;

import src.resource.CommonMethods;
import src.resource.PublicResource;
import src.service.ClientService;



/**
 * @author Guillermo Exposito
 */
public class AddMailBox extends JPanel {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private AddMailBoxController controller;
	private static AddMailBox instance=null;
	private final String  PATH=PublicResource.getPath();
	
	
	
	private AddMailBox(AddMailBoxController controller) {
		this.controller=controller;
		initComponents();
		btnSave.addActionListener(new ActionSaveMailBox());
		btnSearch.addActionListener(new ActionSearchUsers());
		btnAdd.addActionListener(new ActionAddItem());
		btnRemove.addActionListener(new ActionRemoveItem());
		this.setUnassignedPostOffices();
		
	}
	
	public static AddMailBox getInstance(AddMailBoxController controller){
		if(instance==null)
			instance=new AddMailBox(controller);
		return instance;
	}
	

	private void initComponents() {
		// JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
		// Generated using JFormDesigner Evaluation license - Guillermo Exposito
		panel2 = new JPanel();
		lblTitle = new JLabel();
		panel1 = new JPanel();
		jPanelSearchPostOffce = new JPanel();
		lblSearchPostOffice = new JLabel();
		label1 = new JLabel();
		txtSearch = new JTextField();
		btnSearch = new JButton();
		scrollPane1 = new JScrollPane();
		jListPostUser = new JList();
		jPanelTrustRelationships = new JPanel();
		label2 = new JLabel();
		scrollPane2 = new JScrollPane();
		jListAddedPostOffices = new JList();
		btnAdd = new JButton();
		btnRemove = new JButton();
		label3 = new JLabel();
		scrollPane3 = new JScrollPane();
		jListsAvailablePostOffices = new JList();
		this2 = new JPanel();
		jPanelFields = new JPanel();
		lblName = new JLabel();
		txtName = new JTextField();
		label4 = new JLabel();
		label6 = new JLabel();
		passwordFieldPass = new JPasswordField();
		label8 = new JLabel();
		label7 = new JLabel();
		passwordFieldConfirmPass = new JPasswordField();
		label9 = new JLabel();
		label10 = new JLabel();
		chkActive = new JCheckBox();
		label5 = new JLabel();
		btnSave = new JButton();
		CellConstraints cc = new CellConstraints();

		//======== this ========

		setLayout(new FormLayout(
			"left:default:grow",
			"fill:default, 120dlu, fill:default:grow"));

		//======== panel2 ========
		{
			panel2.setLayout(new FormLayout(
				"left:420dlu",
				"10dlu, fill:default"));

			//---- lblTitle ----
			lblTitle.setText("Add MailBox");
			lblTitle.setFont(new Font("Dialog", Font.PLAIN, 18));
			panel2.add(lblTitle, cc.xywh(1, 2, 1, 1, CellConstraints.CENTER, CellConstraints.DEFAULT));
		}
		add(panel2, cc.xy(1, 1));

		//======== panel1 ========
		{
			panel1.setLayout(new HorizontalLayout(1));

			//======== jPanelSearchPostOffce ========
			{
				jPanelSearchPostOffce.setLayout(new FormLayout(
					"20dlu, 22dlu, 3dlu, 100dlu, 3dlu, $lcgap, 62dlu, $lcgap, 5dlu",
					"default, 5dlu, default, 10dlu, fill:default:grow, 10dlu"));

				//---- lblSearchPostOffice ----
				lblSearchPostOffice.setText("User owner");
				lblSearchPostOffice.setFont(new Font("Dialog", Font.PLAIN, 14));
				jPanelSearchPostOffce.add(lblSearchPostOffice, cc.xywh(2, 1, 3, 1));

				//---- label1 ----
				label1.setText("Name");
				jPanelSearchPostOffce.add(label1, cc.xywh(2, 3, 1, 1, CellConstraints.RIGHT, CellConstraints.DEFAULT));
				jPanelSearchPostOffce.add(txtSearch, cc.xy(4, 3));

				//---- btnSearch ----
				btnSearch.setText("Search");
				jPanelSearchPostOffce.add(btnSearch, cc.xywh(7, 3, 3, 1, CellConstraints.LEFT, CellConstraints.DEFAULT));

				//======== scrollPane1 ========
				{

					//---- jListUser ----
					jListPostUser.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
					scrollPane1.setViewportView(jListPostUser);
				}
				jPanelSearchPostOffce.add(scrollPane1, cc.xywh(2, 5, 6, 1, CellConstraints.FILL, CellConstraints.FILL));
			}
			panel1.add(jPanelSearchPostOffce);

			//======== jPanelTrustRelationships ========
			{
				jPanelTrustRelationships.setBorder(null);
				jPanelTrustRelationships.setLayout(new FormLayout(
					"10dlu, 96dlu, 97dlu, 5dlu, 10dlu",
					"28dlu, default, 3dlu, fill:default:grow, 2*($lgap, default), $lgap, fill:default:grow, 10dlu"));

				//---- label2 ----
				label2.setText("Assigned Post Offices");
				label2.setFont(label2.getFont().deriveFont(label2.getFont().getSize() + 2f));
				jPanelTrustRelationships.add(label2, cc.xy(2, 2));

				//======== scrollPane2 ========
				{

					//---- jListAddedPostOffices ----
					jListAddedPostOffices.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
					scrollPane2.setViewportView(jListAddedPostOffices);
				}
				jPanelTrustRelationships.add(scrollPane2, cc.xywh(2, 4, 2, 1, CellConstraints.DEFAULT, CellConstraints.FILL));

				//---- btnAdd ----
				btnAdd.setText("Add");
				jPanelTrustRelationships.add(btnAdd, cc.xywh(2, 6, 1, 1, CellConstraints.CENTER, CellConstraints.DEFAULT));

				//---- btnRemove ----
				btnRemove.setText("Remove");
				jPanelTrustRelationships.add(btnRemove, cc.xywh(3, 6, 1, 1, CellConstraints.CENTER, CellConstraints.DEFAULT));

				//---- label3 ----
				label3.setText("Unassigned Post Offices");
				label3.setFont(label3.getFont().deriveFont(label3.getFont().getSize() + 2f));
				jPanelTrustRelationships.add(label3, cc.xy(2, 8));

				//======== scrollPane3 ========
				{

					//---- jListsAvailablePostOffices ----
					jListsAvailablePostOffices.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
					scrollPane3.setViewportView(jListsAvailablePostOffices);
				}
				jPanelTrustRelationships.add(scrollPane3, cc.xywh(2, 10, 2, 1, CellConstraints.DEFAULT, CellConstraints.FILL));
			}
			panel1.add(jPanelTrustRelationships);
		}
		add(panel1, cc.xy(1, 3));

		//======== this2 ========
		{
			this2.setLayout(new BoxLayout(this2, BoxLayout.Y_AXIS));

			//======== jPanelFields ========
			{
				jPanelFields.setLayout(new FormLayout(
					"10dlu, $lcgap, 56dlu, $lcgap, 100dlu, 5dlu, 10dlu",
					"10dlu, 6*(default, $lgap), 10dlu"));

				//---- lblName ----
				lblName.setText("Name");
				lblName.setHorizontalAlignment(SwingConstants.RIGHT);
				jPanelFields.add(lblName, cc.xy(3, 2));
				jPanelFields.add(txtName, cc.xy(5, 2));

				//---- label4 ----
				label4.setText("*");
				label4.setForeground(Color.red);
				jPanelFields.add(label4, cc.xy(6, 2));

				//---- label6 ----
				label6.setText("Pass");
				jPanelFields.add(label6, cc.xywh(3, 4, 1, 1, CellConstraints.RIGHT, CellConstraints.DEFAULT));
				jPanelFields.add(passwordFieldPass, cc.xy(5, 4));

				//---- label8 ----
				label8.setText("*");
				label8.setForeground(Color.red);
				jPanelFields.add(label8, cc.xy(6, 4));

				//---- label7 ----
				label7.setText("Confirm pass");
				jPanelFields.add(label7, cc.xywh(3, 6, 1, 1, CellConstraints.RIGHT, CellConstraints.DEFAULT));
				jPanelFields.add(passwordFieldConfirmPass, cc.xy(5, 6));

				//---- label9 ----
				label9.setText("*");
				label9.setForeground(Color.red);
				jPanelFields.add(label9, cc.xy(6, 6));

				//---- label10 ----
				label10.setText("State");
				jPanelFields.add(label10, cc.xywh(3, 8, 1, 1, CellConstraints.RIGHT, CellConstraints.DEFAULT));

				//---- chkActive ----
				chkActive.setText("active");
				jPanelFields.add(chkActive, cc.xy(5, 8));

				//---- label5 ----
				label5.setText("* Required fields");
				label5.setForeground(Color.red);
				jPanelFields.add(label5, cc.xy(5, 10));

				//---- btnSave ----
				btnSave.setText("Save");
				jPanelFields.add(btnSave, cc.xywh(5, 12, 1, 1, CellConstraints.RIGHT, CellConstraints.DEFAULT));
			}
			this2.add(jPanelFields);
		}
		add(this2, cc.xy(1, 2));
		// JFormDesigner - End of component initialization  //GEN-END:initComponents
		
		lblTitle.setIcon(new ImageIcon(PATH+"mail-message-new.png"));
		btnSave.setIcon(new ImageIcon(PATH+"document-save.png"));
		btnSearch.setIcon(new ImageIcon(PATH+"system-search.png"));
		btnAdd.setIcon(new ImageIcon(PATH+"go-up.png"));
		btnRemove.setIcon(new ImageIcon(PATH+"go-down.png"));
		btnSearch.setIcon(new ImageIcon(PATH+"system-search.png"));
		
		this.listModelAddedPostOffices=new DefaultListModel();
		this.listModelAvailablePostOffices=new DefaultListModel();
		this.listModelUsers=new DefaultListModel();
	}

	// JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
	// Generated using JFormDesigner Evaluation license - Guillermo Exposito
	private JPanel panel2;
	private JLabel lblTitle;
	private JPanel panel1;
	private JPanel jPanelSearchPostOffce;
	private JLabel lblSearchPostOffice;
	private JLabel label1;
	private JTextField txtSearch;
	private JButton btnSearch;
	private JScrollPane scrollPane1;
	private JList jListPostUser;
	private JPanel jPanelTrustRelationships;
	private JLabel label2;
	private JScrollPane scrollPane2;
	private JList jListAddedPostOffices;
	private JButton btnAdd;
	private JButton btnRemove;
	private JLabel label3;
	private JScrollPane scrollPane3;
	private JList jListsAvailablePostOffices;
	private JPanel this2;
	private JPanel jPanelFields;
	private JLabel lblName;
	private JTextField txtName;
	private JLabel label4;
	private JLabel label6;
	private JPasswordField passwordFieldPass;
	private JLabel label8;
	private JLabel label7;
	private JPasswordField passwordFieldConfirmPass;
	private JLabel label9;
	private JLabel label5;
	private JButton btnSave;
	private JLabel label10;
	private JCheckBox chkActive;
	// JFormDesigner - End of variables declaration  //GEN-END:variables

	private DefaultListModel listModelUsers,listModelAddedPostOffices,listModelAvailablePostOffices;


	public JTextField getTxtSearch() {
		return txtSearch;
	}

	public void setTxtSearch(JTextField txtSearch) {
		this.txtSearch = txtSearch;
	}

	public JButton getBtnSearch() {
		return btnSearch;
	}

	public void setBtnSearch(JButton btnSearch) {
		this.btnSearch = btnSearch;
	}

	public JList getjListPostUser() {
		return jListPostUser;
	}

	public void setjListPostUser(JList jListPostUser) {
		this.jListPostUser = jListPostUser;
	}

	public JList getjListAddedPostOffices() {
		return jListAddedPostOffices;
	}

	public void setjListAddedPostOffices(JList jListAddedPostOffices) {
		this.jListAddedPostOffices = jListAddedPostOffices;
	}

	public JButton getBtnAdd() {
		return btnAdd;
	}

	public void setBtnAdd(JButton btnAdd) {
		this.btnAdd = btnAdd;
	}

	public JButton getBtnRemove() {
		return btnRemove;
	}

	public void setBtnRemove(JButton btnRemove) {
		this.btnRemove = btnRemove;
	}

	public JList getjListAvailablePostOffices() {
		return jListsAvailablePostOffices;
	}

	public void setjListsAvailablePostOffices(JList jListsAvailablePostOffices) {
		this.jListsAvailablePostOffices = jListsAvailablePostOffices;
	}

	public JTextField getTxtName() {
		return txtName;
	}

	public void setTxtName(JTextField txtName) {
		this.txtName = txtName;
	}

	public JPasswordField getPasswordFieldPass() {
		return passwordFieldPass;
	}

	public void setPasswordFieldPass(JPasswordField passwordFieldPass) {
		this.passwordFieldPass = passwordFieldPass;
	}

	public JPasswordField getPasswordFieldConfirmPass() {
		return passwordFieldConfirmPass;
	}

	public void setPasswordFieldConfirmPass(JPasswordField passwordFieldConfirmPass) {
		this.passwordFieldConfirmPass = passwordFieldConfirmPass;
	}

	public JButton getBtnSave() {
		return btnSave;
	}

	public void setBtnSave(JButton btnSave) {
		this.btnSave = btnSave;
	}
	
	public DefaultListModel getListModelUsers() {
		return listModelUsers;
	}

	public void setListModelUsers(DefaultListModel listModelUsers) {
		this.listModelUsers = listModelUsers;
	}

	public DefaultListModel getListModelAddedPostOffices() {
		return listModelAddedPostOffices;
	}

	public void setListModelAddedPostOffices(
			DefaultListModel listModelAddedPostOffices) {
		this.listModelAddedPostOffices = listModelAddedPostOffices;
	}

	public DefaultListModel getListModelAvailablePostOffices() {
		return listModelAvailablePostOffices;
	}

	public void setListModelAvailablePostOffices(
			DefaultListModel listModelAvailablePostOffices) {
		this.listModelAvailablePostOffices = listModelAvailablePostOffices;
	}
	
	public JCheckBox getChkActive() {
		return chkActive;
	}

	public void setChkActive(JCheckBox chkActive) {
		this.chkActive = chkActive;
	}

	private class ActionSaveMailBox implements ActionListener {
		public void actionPerformed(ActionEvent e) {

			try {
				controller.performSaveMailBox();
				//DialogResource.showSaveSucessfully(btnSave.getLocation());
				setUnassignedPostOffices();
				
			} catch (RemoteException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

		}
	}
	
	private class ActionSearchUsers implements ActionListener {
		public void actionPerformed(ActionEvent e) {

			try {
				controller.performSearchUsers();
				//DialogResource.showSaveSucessfully(btnSave.getLocation());
				
			} catch (RemoteException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

		}
	}
	
	private void setUnassignedPostOffices(){
		try {
			this.listModelAddedPostOffices.removeAllElements();
			this.listModelAvailablePostOffices.removeAllElements();
			this.jListAddedPostOffices.setModel(this.listModelAddedPostOffices);
			this.listModelAvailablePostOffices=CommonMethods.toListModel(ClientService.getInstance().getAllPostOffices());
			this.jListsAvailablePostOffices.setModel(listModelAvailablePostOffices);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private class ActionAddItem implements ActionListener {
		public void actionPerformed(ActionEvent e) {
				controller.performAdd();
		}
	}
	
	private class ActionRemoveItem implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			controller.performRemove();
		}
	}
	
	

	
	
}